﻿using HtmlParserLib;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfMonitor
{

    public partial class MainWindow : Window
    {
        Crawler crawler = null;

        ObservableCollection<ReportEventArgs> source;

        public MainWindow()
        {
            InitializeComponent();
            crawler = new Crawler();
            crawler.OnReport += crawler_OnReport;
            if (source == null)
                source = new ObservableCollection<ReportEventArgs>();

            this.grid.ItemsSource = source;

        }

        void crawler_OnReport(object sender, ReportEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() => {
                if (source.Count > 10000)
                {
                    source.Clear();
                }

                source.Insert(0, e);

            });            
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            crawler.Start();
            this.Start.IsEnabled = false;
            this.Stop.IsEnabled = true;
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            crawler.Stop();

            this.Start.IsEnabled = true;
            this.Stop.IsEnabled = false;
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            source.Clear();
        }
    }
}
